package ce.com.newbluedemo.k2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.newbluedemo.R;

/**
 * Created by YaoBinkun on 2016/12/28.
 */

public class ActivityGSGate extends K2BaseActivity {
    private boolean GsStateOpen = false;
    private Button mBtnGate;
    private TextView mTvGsState;
    private final String OPEN = "已打开";
    private final String CLOSE = "已关闭";
    private CEDevK2Proxy mK2Proxy;
    private Handler mK2Handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0){
                showToast("设置失败，请稍后重试！");
            } else {
                showToast("设置成功！");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gs_gate);
        setTitle("发送数据开关");
        mBtnGate = (Button) findViewById(R.id.btn_gate);
        mTvGsState = (TextView) findViewById(R.id.tv_gs_state);
        mK2Proxy = BleFactory.getInstance().getK2Proxy();
        mK2Proxy.setHandler(mK2Handler);
        mK2Proxy.setHandler(mK2Handler);
        mBtnGate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mK2Proxy.isConnectOK()) {  //判断蓝牙连接状态，同理可用isDisconnet()方法判断
                    Toast.makeText(ActivityGSGate.this, "未连接设备，请重试!", Toast.LENGTH_SHORT).show();
                    return;
                }
                GsStateOpen = !GsStateOpen;
                mK2Proxy.getSendHelper().setEnableGsDataTrans(GsStateOpen);
                if (GsStateOpen) {
                    mBtnGate.setText("关闭");
                    mTvGsState.setText(OPEN);
                } else {
                    mBtnGate.setText("打开");
                    mTvGsState.setText(CLOSE);
                }
            }
        });

    }

    private void showToast(String input) {
        Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    }
}
